package com.zkca.xf;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;

import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.tencent.android.tpush.XGIOperateCallback;
import com.tencent.android.tpush.XGPushConfig;
import com.tencent.android.tpush.XGPushManager;
import com.tencent.bugly.crashreport.CrashReport;
import com.videogo.openapi.EZOpenSDK;
import com.zkca.xf.model.javabean.uploadbean.RequestInspect;
import com.zkca.xf.model.javabean.uploadbean.RequestPage;
import com.zkca.xf.utils.LogUtil;
import com.zkca.xf.utils.Md5Util;
import com.zkca.xf.utils.SPUtil;

import java.io.File;
import java.util.Stack;

/**
 * Created by li on 2018/7/30.
 */

public class MyApplication extends Application {
    private Stack<Activity> activityStack;

    public static MyApplication instance;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;


        // 在使用 SDK 各组间之前初始化 context 信息，传入 ApplicationContext
        SDKInitializer.initialize(this);
        //自4.3.0起，百度地图SDK所有接口均支持百度坐标和国测局坐标，用此方法设置您使用的坐标类型.
        //包括BD09LL和GCJ02两种坐标，默认是BD09LL坐标。
        SDKInitializer.setCoordType(CoordType.BD09LL);

        /** * sdk日志开关，正式发布需要去掉 */
        EZOpenSDK.showSDKLog(true);
        /** * 设置是否支持P2P取流,详见api */
        EZOpenSDK.enableP2P(false);

        /** * APP_KEY请替换成自己申请的   2781ffc6738943fa9d9b9bbc84a0f675*/
        EZOpenSDK.initLib(this, "059ceee36b2c4a8fb56587a860f2ad8b");


        XGPushConfig.setMiPushAppId(getApplicationContext(), "2882303761517930007");
        XGPushConfig.setMiPushAppKey(getApplicationContext(), "5901793060007");
//打开第三方推送
        XGPushConfig.enableOtherPush(getApplicationContext(), true);
//        //设置魅族APPID和APPKEY
//        XGPushConfig.enableOtherPush(getApplicationContext(), true);
//        XGPushConfig.setMzPushAppId(this, "1003636");
//        XGPushConfig.setMzPushAppKey(this, "d3a14bc156e245e1a8b2f9e3492f6c26");
//
//        XGPushConfig.enableDebug(this,true);

        XGPushManager.registerPush(this, new XGIOperateCallback() {
            @Override
            public void onSuccess(Object o, int i) {
                LogUtil.e("Token是",o+"");
                SPUtil.putString(getApplicationContext(), Constants_SP.XGTOKEN, o+"");

            }

            @Override
            public void onFail(Object o, int i, String s) {
                LogUtil.e("注册失败，",s+"<><>"+o+"错误码"+i);
            }
        });

        CrashReport.initCrashReport(getApplicationContext(), "e2b54acd96", true);//bugly配置

    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
        //StubAppUtils.attachBaseContext(this);
        //StubAppUtils.attachBaseContext(context);
    }
    public static EZOpenSDK getOpenSDK() {
        return EZOpenSDK.getInstance();
    }
    public static synchronized MyApplication getInstance() {
        return instance;
    }

    /**
     * 保存Activity的引用
     */
    public void addActivity(Activity act) {
        if (activityStack == null)
            activityStack = new Stack<>();
        activityStack.add(act);
    }

    /**
     * 关闭指定Activity的引用
     */
    public void removeActivity(Activity act) {
        if (activityStack.contains(act))
            activityStack.remove(act);
    }

    /**
     * 获取当前Activity(堆栈中的最后一个)
     */
    public Activity currentActivity() {
        Activity activity = activityStack.lastElement();
        return activity;
    }

    public String getCachePath() {
        File cacheDir;
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
            cacheDir = getExternalCacheDir();
        else
            cacheDir = getCacheDir();
        if (!cacheDir.exists())
            cacheDir.mkdirs();
        return cacheDir.getAbsolutePath();
    }


    /**
     * 退出App
     */
    public void exitApp() {
        if (activityStack != null) {
            synchronized (activityStack) {
                for (Activity act : activityStack) {
                    act.finish();
                }
            }
        }
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

    //巡点相关签名获取
    public static String generateDvSign(RequestInspect req) {
        String result = null;
        StringBuilder sb = new StringBuilder();
        sb.append("#");
        sb.append("Tqp5$%HzzYrP#!00098yyYL-");
        sb.append("#");
        sb.append(req.getAccessId() + req.getAccessId());
        sb.append("#");
        sb.append(req.getAction()*990 - 32786);
        sb.append("#");
        result = sb.toString();
        for (int k = 0; k < 4; k++) {
            result = Md5Util.md5Sum(result);
        }
        return result;
    }
    //获取加密签名
    public String generateSign(RequestPage req) {
        String result = null;
        StringBuilder sb = new StringBuilder();
        sb.append("#");
        sb.append(ConstantsXF.APIKEY);
        sb.append("#");
        sb.append(req.getAccessId() + req.getAccessId());
        sb.append("#");
        sb.append(req.getPageNum()*100/33 + 99876);
        sb.append("#");
        sb.append(req.getPageNum()%7 - 65531);
        for(int i = 0; i< req.getPageNum(); i++) {
            sb.append(req.getTimeStamp());
            sb.append("#");
        }
        result = sb.toString();
        for( int k = 0; k < ConstantsXF.APICOUNT; k++) {
            result = Md5Util.md5Sum(result);
        }
        return result;
    }
}
